package com.eatrip.dao.implementation;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.eatrip.bean.DatiLocale;
import com.eatrip.bean.Ricetta;
import com.eatrip.dao.RicetteDAO;

@Repository
@Transactional
public class RicetteDAOImple implements RicetteDAO {
	
	private HibernateTemplate hibernateTemplate;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Ricetta getRicetta(int idRicetta) {
		List<Ricetta> list = hibernateTemplate.find("FROM Ricetta WHERE idRicette=?",idRicetta);
		if (list.size() == 0)
			return null;
		return list.get(0);
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public Ricetta getRandRicetta() {
		List<Ricetta> list = hibernateTemplate.find("FROM Ricetta ORDER BY RAND()");
		if (list.size() == 0)
			return null;
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ricetta> getAllRicette(int start, int numRow) {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Ricetta.class);
			criteria.setFirstResult(start);
			criteria.setMaxResults(numRow);
		List<Ricetta> list = criteria.list();
		if (list.size() == 0)
			return null;
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ricetta> getRicetteByIdLocale(int idLocale, int start,
			int numRow) {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Ricetta.class);
			criteria.add(Restrictions.eq("idLocaleFk", idLocale));
			criteria.addOrder(Order.desc("idRicetta"));
			criteria.setFirstResult(start);
			criteria.setMaxResults(numRow);
		List<Ricetta> list = criteria.list();
		if (list.size() == 0)
			return null;
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ricetta> getRicetteByIdLocale(DatiLocale locale, int start,
			int numRow) {
		
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Ricetta.class);
		criteria.add(Restrictions.eq("datiLocale", locale));
		criteria.addOrder(Order.desc("idRicetta"));
		criteria.setFirstResult(start);
		criteria.setMaxResults(numRow);
		List<Ricetta> list = criteria.list();
		if (list.size() == 0)
			return null;
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Ricetta> getRicetteByNome(String nome, int start, int numRow) {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Ricetta.class);
			criteria.add(Restrictions.eq("nome", nome));
			criteria.setFirstResult(start);
			criteria.setMaxResults(numRow);
			List<Ricetta> list = criteria.list();
		if (list.size() == 0)
			return null;
		return list;
	}

	@Override
	public void insertRicetta(Ricetta ricetta) {
		hibernateTemplate.save(ricetta);
	}

	@Override
	public void updateRicetta(Ricetta ricetta) {
		hibernateTemplate.update(ricetta);
	}

	@Override
	public void deleteRicetta(Ricetta ricetta) {
		hibernateTemplate.delete(ricetta);
	}

	@Override
	public void deleteAllRicette() {
		Query q = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery("DELETE FROM Ricetta");
		int row = q.executeUpdate();
		if (row == 0)
			System.out.println("errore nella cancellazione di tutte le ricette");
	}

	@Override
	public void deleteRicetteByIdLocale(int idLocale) {
		hibernateTemplate.getSessionFactory().getCurrentSession().delete("FROM Ricetta WHERE idLocaleFk=?", idLocale);
	}

}
